﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PDFWriter
{
    public class PDFStream : INode
    {
        private string xmlString = "<Stream>" +
              "BT\n" +
              "/F1 24 Tf\n" +
              "1 0 0 1 260 600 Tm\n" +
              "(Hello World)Tj\n" +
              "ET\n" +
            "</Stream>";
        public const string STREAM_TAG = "Stream";
        public const string OBJECT_ID = "id";
        public const string GENERATION_ID = "GenID";
        public const string START_TAG = "stream";
        public const string END_TAG = "endstream";

        private XmlDocument reader = new XmlDocument();
        public PDFStream(string xmlStream)
        {
            this.xmlString = xmlStream;
            reader.LoadXml(xmlString);
        }

        public PDFStream()
        {
            reader.LoadXml(xmlString);
        }
       
        private string ProcesXML()
        {
            string result = null;            
            if (reader.FirstChild.Name == STREAM_TAG)
            {

                if (!reader.FirstChild.ChildNodes[0].HasChildNodes)
                {
                    result = reader.InnerText;
                }
                else
                { 
                    foreach (XmlNode node in reader.ChildNodes)
	                {
                        result += Utility.GetPDFString(node.InnerXml);        
	                }
                
                }                
                result = START_TAG+"\n"+result+"\n"+END_TAG;
            }
            return result+"\n";
        }

        public PDFObject GetPDFObject()
        {
            return new PDFObject() { Text  = ProcesXML()};
        }

        public List<PDFObject> GetPDFObjects()
        {
            return new List<PDFObject>(){new PDFObject() { Text = ProcesXML() }};
        }
    }
}
